package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserDTO;
import util.UrlUtility;
import bizlogic.UserManager;

@WebServlet("/ProcessUser")
public class ProcessUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ProcessUserServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doShow(request, response);
	}

	private void doShow(HttpServletRequest req, HttpServletResponse res) {
		String address = "/WEB-INF/ManageUser.jsp";
		String action = req.getParameter("action");
		
		UserDTO data = null;
		UserManager um = new UserManager();
		
		ArrayList<UserDTO> managers = um.getManagers(); 
		req.setAttribute("managers", managers);
		
		if ( action!= null && action.trim().equalsIgnoreCase("new") ) {
			data = new UserDTO();
			
			req.setAttribute("action", "new");
			req.setAttribute("data", data);
			
			UrlUtility.forwardPage(req, res, address);
		} else if ( action != null && action.trim().equalsIgnoreCase("edit") ) {
			boolean valid = true;
			String id;
			
			id = req.getParameter("id");
			data = um.getUser(id);
			if ( data == null ) valid = false;
			
			
			if ( valid ) {
				req.setAttribute("action", "edit");
				req.setAttribute("data", data);
				
				UrlUtility.forwardPage(req, res, address);
			} else {
				UrlUtility.redirectPage(res, "LoadUser");
			}
		} else {
			UrlUtility.redirectPage(res, "LoadUser");
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	private void processRequest(HttpServletRequest request,HttpServletResponse response)
	{
		UserManager um = new UserManager();
		
		String action = "", error = "";
		boolean valid = true;
		
		ArrayList<UserDTO> managers = um.getManagers(); 
		request.setAttribute("managers", managers);
		
		// Validation
		UserDTO data = new UserDTO();
		
		try {
			data.setId(request.getParameter("id"));
			data.setName(request.getParameter("name"));
			data.setType(request.getParameter("role"));
			data.setPassword(request.getParameter("password"));
			data.setEmail(request.getParameter("email"));
			data.setManagerId(request.getParameter("manager"));
			data.setStatus(Integer.parseInt(request.getParameter("status")));
			
			//System.out.println(data);
			
			action = request.getParameter("action");
			
			if ( action.trim().equalsIgnoreCase("new") ) {
				error = um.insertUser(data);
			} else if ( action.trim().equalsIgnoreCase("edit") ) {
				error = um.updateUser(data);
			}
			
			if ( !error.trim().equalsIgnoreCase("") ) valid = false;
		} catch ( NumberFormatException e ) {
			valid = false;
			error = "user.error.invalidnumberstatus";
		}
		
		if ( valid ) {
			UrlUtility.redirectPage(response, "LoadUser");
		} else {
			request.setAttribute("action", action);
			request.setAttribute("error", error);
			request.setAttribute("data", data);
			
			UrlUtility.forwardPage(request, response, "/WEB-INF/ManageUser.jsp");
		}
	}
}
